/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InvoiceCreateView.java
 *
 * Created on Apr 3, 2011, 11:42:37 PM
 */

package views;

import controllers.InvoiceController;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import models.Customer;
import models.Invoice;
import models.Order;

/**
 *
 * @author pelle
 */
public class InvoiceCreateView extends AbstractView {

    private InvoiceController invoiceCtr = new InvoiceController();

    private ArrayList<Order> availableOrders = new ArrayList<Order>();
    private ArrayList<Order> selectedOrders = new ArrayList<Order>();
    private float selectedTotal = 0;

    /** Creates new form InvoiceCreateView */
    public InvoiceCreateView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        selectCustomer = new javax.swing.JButton();
        cusName = new javax.swing.JLabel();
        cusPhone = new javax.swing.JLabel();
        cusCity = new javax.swing.JLabel();
        cusAddress = new javax.swing.JLabel();
        cusZipcode = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        ordreCount = new javax.swing.JLabel();
        subTotal = new javax.swing.JLabel();
        moms = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        availableList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        selectedList = new javax.swing.JList();
        addToInvoiceBtn = new javax.swing.JButton();
        removeFromInvoiceBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        createFaktura = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Vælg kunde"));

        selectCustomer.setText("Vælg kunde");
        selectCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectCustomerActionPerformed(evt);
            }
        });

        cusName.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        cusName.setText("<Ingen kunde valgt>");

        cusPhone.setText(" ");

        cusCity.setText(" ");

        cusAddress.setText(" ");

        cusZipcode.setText(" ");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 264, Short.MAX_VALUE)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(cusName)
                    .add(cusAddress)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(cusZipcode)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cusCity))
                    .add(selectCustomer)
                    .add(cusPhone))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 154, Short.MAX_VALUE)
            .add(jPanel1Layout.createSequentialGroup()
                .add(cusName)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cusAddress)
                .add(8, 8, 8)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cusZipcode)
                    .add(cusCity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cusPhone)
                .add(9, 9, 9)
                .add(selectCustomer))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Faktura overblik"));

        jLabel3.setText("Ordre i faktura:");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabel4.setText("Subtotal:");

        jLabel5.setText("Moms (25%):");

        jLabel6.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabel6.setText("Total:");

        ordreCount.setText("0");

        subTotal.setText("0");

        moms.setText("0");

        total.setText("0");

        jLabel11.setText("kr.");

        jLabel12.setText("kr.");

        jLabel13.setText("kr.");

        jLabel14.setText("stk.");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jLabel4)
                    .add(jLabel5)
                    .add(jLabel6))
                .add(18, 18, 18)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(total)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel11))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(moms)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel12))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(subTotal)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel13))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(ordreCount)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel14)))
                .addContainerGap(116, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(12, 12, 12)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(ordreCount)
                    .add(jLabel14))
                .add(18, 18, 18)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(subTotal)
                    .add(jLabel13))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(moms)
                    .add(jLabel12))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(total)
                    .add(jLabel11))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ordre"));

        availableList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(availableList);

        selectedList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(selectedList);

        addToInvoiceBtn.setText(">");
        addToInvoiceBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToInvoiceBtnActionPerformed(evt);
            }
        });

        removeFromInvoiceBtn.setText("<");
        removeFromInvoiceBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeFromInvoiceBtnActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabel1.setText("Ikke faktureret ordre:");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabel2.setText("Tilføjet til faktura:");

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 210, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(removeFromInvoiceBtn)
                            .add(addToInvoiceBtn)))
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 237, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(35, 35, 35)
                        .add(addToInvoiceBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeFromInvoiceBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        createFaktura.setText("Opret faktura");
        createFaktura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createFakturaActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(createFaktura)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(createFaktura)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectCustomerActionPerformed
        new CustomerSelectView(this).setVisible(true);
}//GEN-LAST:event_selectCustomerActionPerformed

    private void createFakturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createFakturaActionPerformed
        if(invoiceCtr.getCustomer() != null)
        {
            if(selectedOrders.size() > 0)
            {
                if(this.showConfirm("Er du sikker på at du vil oprette fakturaen?"))
                {
                    invoiceCtr.addOrders(selectedOrders);
                    int invoiceId = invoiceCtr.createInvoice();

                    if(invoiceId > 0) {
                        this.showMessage("Fakturaen blev oprettet med fakturanummeret: #" + invoiceId);
                    } else {
                        this.showError("Fakturaen kunne ikke blive oprettet!");
                    }
                }
            } else {
                this.showError("Mindst én ordre skal være i fakturaen!");
            }
        } else {
            this.showError("Du skal vælge en kunde!");
        }
    }//GEN-LAST:event_createFakturaActionPerformed

    private void addToInvoiceBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToInvoiceBtnActionPerformed
        int selected = this.availableList.getSelectedIndex();
        if(selected >= 0) {
            Order order = this.availableOrders.get(selected);
            if(order != null) {
                this.selectedOrders.add(order);
                this.availableOrders.remove(order);
            }
        }

        updateFields();
        updateDetails();
    }//GEN-LAST:event_addToInvoiceBtnActionPerformed

    private void removeFromInvoiceBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeFromInvoiceBtnActionPerformed
        int selected = this.selectedList.getSelectedIndex();
        if(selected >= 0) {
            Order order = this.selectedOrders.get(selected);
            if(order != null) {
                this.selectedOrders.remove(order);
                this.availableOrders.add(order);
            }
        }

        updateFields();
        updateDetails();
    }//GEN-LAST:event_removeFromInvoiceBtnActionPerformed

    public boolean setCustomer(Customer customer) {
        this.invoiceCtr.addCustomer(customer);
        availableOrders = invoiceCtr.getAvalibleOrders();
        selectedOrders = new ArrayList<Order>();
        
        updateFields();
        updateDetails();
        

        return true;
    }

    private void updateFields() {

        DefaultListModel selectedListModel = new DefaultListModel();
        DefaultListModel availableListModel = new DefaultListModel();
        if(this.availableOrders != null) {
            for(Order o : this.availableOrders) {
                availableListModel.addElement("#" + o.getId() + ": " + o.getTotalPrice() + " kr.");
            }
        }

        this.selectedTotal = 0;
        if(this.selectedOrders != null) {
            for(Order o : this.selectedOrders) {
                selectedListModel.addElement("#" + o.getId() + ": " + o.getTotalPrice() + " kr.");
                selectedTotal += o.getTotalPrice();
            }
        }

        selectedList.removeAll();
        selectedList.setModel(selectedListModel);
        
        availableList.removeAll();
        availableList.setModel(availableListModel);
    }

    private void updateDetails() {
        Customer customer = invoiceCtr.getInvoice().getCustomer();
        if(customer != null) {
            double p = selectedTotal;
            double vat = p*0.25;

            ordreCount.setText("" + selectedOrders.size());

            cusName.setText(customer.getName());
            cusAddress.setText(customer.getAddress());
            cusZipcode.setText("" + customer.getZipcode());
            cusCity.setText(customer.getCity());
            cusPhone.setText("" + customer.getPhoneno());

            subTotal.setText("" + p);
            moms.setText("" + vat);
            total.setText("" + (p + vat));
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addToInvoiceBtn;
    private javax.swing.JList availableList;
    private javax.swing.JButton createFaktura;
    private javax.swing.JLabel cusAddress;
    private javax.swing.JLabel cusCity;
    private javax.swing.JLabel cusName;
    private javax.swing.JLabel cusPhone;
    private javax.swing.JLabel cusZipcode;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel moms;
    private javax.swing.JLabel ordreCount;
    private javax.swing.JButton removeFromInvoiceBtn;
    private javax.swing.JButton selectCustomer;
    private javax.swing.JList selectedList;
    private javax.swing.JLabel subTotal;
    private javax.swing.JLabel total;
    // End of variables declaration//GEN-END:variables

}
